<?php
declare (strict_types = 1);

namespace app\api\controller;

use app\common\controller\BaseController;
use app\api\model\Collect as CollectModel;
use app\common\utils\JsonResponse;
use think\facade\Request;
use think\facade\Db;

class Collect extends BaseController
{
    /**
     * 添加/取消收藏
     */
    public function toggle()
    {
        try {
            $imgId = Request::param('img_id/d');
            if (empty($imgId)) {
                return JsonResponse::error('图片ID不能为空');
            }
            $userId = request()->userId;          
            
            // 检查是否已收藏
            if (CollectModel::isCollected(intval($userId), intval($imgId))) {
                // 取消收藏
                CollectModel::cancelCollect(intval($userId), intval($imgId));
                return JsonResponse::success('取消收藏成功');
            } else {
                // 添加收藏
                CollectModel::addCollect(intval($userId), intval($imgId));
                return JsonResponse::success('收藏成功');
            }
        } catch (\Exception $e) {
            return JsonResponse::error($e->getMessage());
        }
    }

    /**
     * 获取收藏列表
     */
    public function list()
    {
        try {
            $page = Request::param('page/d', 1);
            $limit = Request::param('limit/d', 10);            
            $userId = request()->userId;
            $result = CollectModel::getList(intval($userId), intval($page), intval($limit));
            
            return JsonResponse::success($result);
        } catch (\Exception $e) {
            return JsonResponse::error($e->getMessage());
        }
    }

    /**
     * 检查是否已收藏
     */
    public function check()
    {
        try {
            $img_id = Request::param('img_id/d');
            if (empty($img_id)) {
                return JsonResponse::error('ID不能为空');
            }
            $userId = request()->userId;
            $isCollected = CollectModel::isCollected(intval($userId), intval($img_id));
            
            return JsonResponse::success([
                'is_collected' => $isCollected
            ]);
        } catch (\Exception $e) {
            return JsonResponse::error($e->getMessage());
        }
    }

    /**
     * 删除收藏
     * @return \think\Response
     */
    public function delete()
    {
        try {
            // 获取收藏ID
            $collectId = Request::param('collect_id/d');
            if (empty($collectId)) {
                return JsonResponse::error('收藏ID不能为空');
            }

            // 获取当前用户ID
            $userId = request()->userId;

            // 查询收藏记录
            $collect = Db::name('collect')
                ->where('id', $collectId)
                ->where('user_id', $userId)
                ->whereNull('delete_time')
                ->find();

            if (!$collect) {
                return JsonResponse::error('收藏记录不存在或已删除', 404);
            }

            // 软删除收藏记录
            $result = Db::name('collect')
                ->where('id', $collectId)
                ->update([
                    'delete_time' => time(),
                    'update_time' => time()
                ]);

            if ($result === false) {
                return JsonResponse::error('删除失败');
            }

            return JsonResponse::success([
                'collect_id' => $collectId
            ], '删除成功');

        } catch (\Exception $e) {
            return JsonResponse::error('删除失败: ' . $e->getMessage());
        }
    }
} 